package com.lijiajia.cloud.common.core.pojo;

import lombok.Data;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;

/**
 * 分页参数对象
 */
@Data
public class CloudPageRequest implements Serializable {
    public static final int DEFAULT_PAGE = 1;
    public static final int DEFAULT_SIZE = 10;
    private int page = DEFAULT_PAGE;
    private int size = DEFAULT_SIZE;
    private Sort sort = Sort.unsorted();

    /**
     * SQL分页(从0开始)
     *
     * @return pageable
     */
    public Pageable sqlPage() {
        return PageRequest.of(page < 1 ? 0 : page - 1, size, sort);
    }

    /**
     * SQL分页(从0开始)
     *
     * @param sort 排序
     * @return pageable
     */
    public Pageable sqlPage(Sort sort) {
        return PageRequest.of(page < 1 ? 0 : page - 1, size, sort);
    }

    /**
     * 页面展示分页(从1开始)
     *
     * @return pageable
     */
    public Pageable viewPage() {
        return PageRequest.of(page < 1 ? 1 : page, size, sort);
    }

}
